'use strict';
module.exports = {
    /**
     * 生成已经可以退的用户保证金
     * @url client/timedTask/pub/test 前端调用的url参数地址
     * data 请求参数
     * @param {String} params1  参数1
     */
    main: async (event) => {
        let { data = {}, userInfo, util, filterResponse, originalParam } = event;
        let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
        let { uid } = data;
        let res = {
            code: 0,
            msg: ""
        };
        // 业务逻辑开始-----------------------------------------------------------
        // await vk.baseDao.del({
        //     dbName:"biddings-refund-oreders",
        //     whereJson:{
        //       _id : _.exists(true),
        //       type:'保证金'
        //     }
        //   });
        // return;
        let now = Date.now();
        let checkdate = new Date('2024-01-19 23:59:59')

        if (now > checkdate) {
            res = await vk.baseDao.selects({
                dbName: "uni-id-users", // 主表名
                pageIndex: 1,
                pageSize: -1,
                getMain: true,
                whereJson: {
                    first_earnest_money_out_trade_no: _.neq(null),
                    // biddingstatus: _.neq(-1)
                },
                foreignDB: [
                    {
                        dbName: "bidding-booths", // 副表名
                        localKey: "_id", // 主表外键字段名
                        foreignKey: "buy_user", // 副表外键字段名
                        as: "buy_booths",
                        fieldJson: {
                            _id: true,
                            name: true,
                            booth_no: true,

                        },
                        whereJson: {
                            status: 3,
                        },
                        limit: 1000,
                    },
                    {
                        dbName: "vk-pay-orders", // 副表名
                        localKey: "first_earnest_money_out_trade_no", // 主表外键字段名
                        foreignKey: "out_trade_no", // 副表外键字段名
                        as: "payorders",
                        fieldJson: {
                            _id: true,
                            total_fee: true,
                            user_id: true,
                            out_trade_no: true,
                            transaction_id: true,
                            transaction_id: true,
                            create_date: true
                        },
                        whereJson: {
                            status: 1,
                            iscreated: null,
                        },
                        limit: 1,
                    }],

                lastWhereJson: {
                    payorders: _.neq(null)
                }
            });

            console.log(res.length)
            let newArr = res.map(x => {

                return {

                    payorder_id: x.payorders._id,
                    out_trade_no: x.payorders.out_trade_no,
                    total_fee: x.payorders.total_fee,
                    buy_booths: x.buy_booths,
                    user_id: x._id,
                    realname: x.realname,
                    mobile: x.mobile,
                    transaction_id: x.payorders.transaction_id,
                    type: '保证金',
                    create_date: x.payorders.create_date,
                    biddingstatus: x.biddingstatus
                }

            })
            for (let index = 0; index < newArr.length; index++) {
                const transaction = await vk.baseDao.startTransaction();
                try {
                    const addModel = newArr[index];
                    let num = await vk.baseDao.updateById({
                        db: transaction,
                        dbName: "vk-pay-orders", // 表名
                        id: addModel.payorder_id,
                        dataJson: { // 需要修改的数据
                            iscreated: 1,
                        }
                    });
                    let dbName = "biddings-refund-oreders";
                     await vk.baseDao.add({
                        db: transaction,
                        dbName,
                        dataJson: {
                            ...addModel
                        }
                    });

                    await transaction.commit();
                }
                catch (err) {
                    // 事务回滚
                    await transaction.rollback();
                    console.error(`transaction error`, err)
                }
            }
            // 业务逻辑结束-----------------------------------------------------------
            return res;
        } else {
            return res
        }

    }
}
